﻿using Biomedica.Graphics;
using Biomedica.Propability.StateMachine;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace KinectWindows.Nui.Nui.Postures
{
    public delegate void OnPostureDetected(String Posturename);

    public interface IPosturable
    {
        void OnPostureDetected(String posturename);
    }
    public abstract class PostureDetectorBase
    {
       
        public Vector3[] OldTemp { set; get; }
        public OnPostureDetected PostureDetected;
         public Timer internal_timer = new Timer(3000);
         private string previousPosture;
        public StateContext Context { set; get; }
        public string CurrentPosture { set; get; }
        


        public PostureDetectorBase(int accumelator,StateContext Context)
        {
            this.Context = Context;
            this.Accumelator = accumelator;
        }
        public virtual void StartTimer()
        {
            internal_timer.Start();
        }
        public virtual void Stop()
        {
            internal_timer.Stop();
        }
        public abstract void TrackPosture(Vector3[] jointsPos);
       // public abstract void TrackPosture(Vector3 head,Vector3 lefthand,Vector3 righthand);
        public virtual void DetectPosture(Vector3[] JointTemplate)
        {
            OldTemp = JointTemplate;
            Context.getStateMachineEngine().Run(Context);
        }


        


        public int Accumelator { get; set; }

        public void RaisPostureDetected(string posture)
        {
            if (Accumelator < AccumelatorTarget)
            {
                if (AccumelatedPosture != posture)
                {
                    Accumelator = 0;
                    AccumelatedPosture = posture;
                }
                Accumelator++;
                return;
            }
            if (previousPosture == posture) return;
            previousPosture = posture;
            if (PostureDetected != null) PostureDetected(posture);
            Accumelator = 0;
        }
        protected void Reset()
        {
            previousPosture = "";
            Accumelator = 0;
        }

        public int AccumelatorTarget { get; set; }

        public string AccumelatedPosture { get; set; }
    }
}
